package com.mia.props.common;

import com.mia.craftstudio.libgdx.Vector3;
import com.mia.craftstudio.minecraft.BlockDimensionalPosition;
import com.mia.craftstudio.minecraft.IAnimatedTile;
import com.mia.props.DecoModelMetadata;
import com.mia.props.Props;
import com.mia.props.network.Msg0x00SwitchRunning;
import com.mia.props.network.NetworkHelper;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagIntArray;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;

public class TileProps extends TileEntity implements IAnimatedTile {
   public int type = 0;
   public int rotation;
   protected boolean isRunning = false;
   public Random rand = new Random();
   public Set slaves = new HashSet();
   public Map tileParams = new HashMap();
   protected boolean initialized = false;
   private BlockDimensionalPosition dimpos = null;

   public void init() {
   }

   public boolean shouldRenderInPass(int pass) {
      return pass == 0 || pass == 1;
   }

   public BlockDimensionalPosition getBlockPosDim() {
      if (this.dimpos == null) {
         this.dimpos = new BlockDimensionalPosition(this.worldObj.provider.dimensionId, this.xCoord, this.yCoord, this.zCoord);
      }

      return this.dimpos;
   }

   public void invalidate() {
      super.invalidate();
      this.dimpos = null;
   }

   public void validate() {
      super.validate();
      this.dimpos = null;
   }

   public void updateContainingBlockInfo() {
      super.updateContainingBlockInfo();
      this.dimpos = null;
   }

   public void readFromNBT(NBTTagCompound compound) {
      super.readFromNBT(compound);
      this.type = compound.getInteger("BlockType");
      this.rotation = compound.getInteger("BlockRotation");
      this.isRunning = compound.hasKey("isRunning") ? compound.getBoolean("isRunning") : false;
      if (this.worldObj != null) {
         TileProps tileProps = this.getNewTileEntityForMeta(this.type, this.rotation, this.isRunning);
         NBTTagList slaves = compound.getTagList("slaves", 11);

         for(int i = 0; i < slaves.tagCount(); ++i) {
            int[] pos = slaves.func_150306_c(i);
            tileProps.slaves.add(new Integer[]{pos[0], pos[1], pos[2]});
         }

         this.worldObj.setTileEntity(this.xCoord, this.yCoord, this.zCoord, tileProps);
         tileProps.init();
      } else {
         NBTTagList slaves = compound.getTagList("slaves", 11);

         for(int i = 0; i < slaves.tagCount(); ++i) {
            int[] pos = slaves.func_150306_c(i);
            this.slaves.add(new Integer[]{pos[0], pos[1], pos[2]});
         }
      }

      this.tileParams = ((DecoModelMetadata)Props.modelData.get(this.type)).tileParams;
   }

   public void writeToNBT(NBTTagCompound compound) {
      super.writeToNBT(compound);
      compound.setInteger("BlockType", this.type);
      compound.setInteger("BlockRotation", this.rotation);
      compound.setBoolean("isRunning", this.isRunning);
      NBTTagList slavesLst = new NBTTagList();
      Iterator var3 = this.slaves.iterator();

      while(var3.hasNext()) {
         Integer[] slave = (Integer[])var3.next();
         NBTTagIntArray pos = new NBTTagIntArray(new int[]{slave[0], slave[1], slave[2]});
         slavesLst.appendTag(pos);
      }

      compound.setTag("slaves", slavesLst);
   }

   public Packet getDescriptionPacket() {
      NBTTagCompound nbttagcompound = new NBTTagCompound();
      this.writeToNBT(nbttagcompound);
      return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, -1, nbttagcompound);
   }

   public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
      this.readFromNBT(pkt.func_148857_g());
      if (FMLCommonHandler.instance().getSide().isClient()) {
         this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
      }

   }

   public TileProps getNewTileEntityForMeta(int meta, int rotation, boolean isRunning) {
      try {
         TileProps tile = (TileProps)((DecoModelMetadata)Props.modelData.get(meta)).tileType.getTileClass().newInstance();
         tile.type = meta;
         tile.rotation = rotation;
         tile.isRunning = isRunning;
         tile.tileParams = ((DecoModelMetadata)Props.modelData.get(meta)).tileParams;
         return tile;
      } catch (InstantiationException var6) {
         var6.printStackTrace();
      } catch (IllegalAccessException var7) {
         var7.printStackTrace();
      } catch (Exception var8) {
         System.out.printf("!!! CRITICAL ERROR : PRB WHILE LOADING TE FOR META %s !!!\n", meta);
         throw new RuntimeException(var8);
      }

      return null;
   }

   public void renderParticles(World world, int x, int y, int z, Random random) {
   }

   public boolean canUpdate() {
      return false;
   }

   public void onBlockClicked(World world, int x, int y, int z, EntityPlayer player) {
   }

   public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float var7, float var8, float var9) {
      return false;
   }

   public int getMetadata() {
      return this.rotation;
   }

   public void switchIsRunning() {
      this.isRunning = !this.isRunning;
      if (!this.worldObj.isRemote) {
         NetworkHelper.INSTANCE.network.sendToDimension(new Msg0x00SwitchRunning(this), this.worldObj.provider.dimensionId);
      }

   }

   public void setIsRunning(boolean state) {
      this.isRunning = state;
      if (!this.worldObj.isRemote) {
         NetworkHelper.INSTANCE.network.sendToDimension(new Msg0x00SwitchRunning(this), this.worldObj.provider.dimensionId);
      }

   }

   public boolean getIsRunning() {
      return this.isRunning;
   }

   public boolean isBed() {
      return false;
   }

   public int getLightValue(IBlockAccess world, int x, int y, int z) {
      return this.getModelData() != null ? this.getModelData().lightLevel : 0;
   }

   public int getSizeInventoryX() {
      return this.getModelData().invX;
   }

   public int getSizeInventoryY() {
      return this.getModelData().invY;
   }

   public void onBlockHarvested(World world, int x, int y, int z, int par5, EntityPlayer par6EntityPlayer) {
   }

   public boolean onItemStackDropSaveNBT() {
      return false;
   }

   public void addSlave(TileFake slave) {
      this.slaves.add(new Integer[]{slave.xCoord, slave.yCoord, slave.zCoord});
   }

   public void rmSlave(TileFake slave) {
      this.slaves.remove(new Integer[]{slave.xCoord, slave.yCoord, slave.zCoord});
      this.worldObj.setBlockToAir(slave.xCoord, slave.yCoord, slave.zCoord);
   }

   public void removeSlaves() {
      Iterator var1 = this.slaves.iterator();

      while(var1.hasNext()) {
         Integer[] slave = (Integer[])var1.next();
         this.worldObj.setBlockToAir(slave[0], slave[1], slave[2]);
      }

   }

   public DecoModelMetadata getModelData() {
      return (DecoModelMetadata)Props.modelData.get(this.type);
   }

   public void markRenderDirty() {
   }

   public boolean sameID(TileEntity tile) {
      if (!(tile instanceof TileProps)) {
         return false;
      } else {
         return this.getModelData().decocraftModelID == ((TileProps)tile).getModelData().decocraftModelID;
      }
   }

   @SideOnly(Side.CLIENT)
   public AxisAlignedBB getRenderBoundingBox() {
      Vector3[] bounds = ((DecoModelMetadata)Props.modelData.get(this.type)).wrapper.getExtend(this.rotation);
      return AxisAlignedBB.getBoundingBox((double)this.xCoord + (double)bounds[0].x, (double)this.yCoord + (double)bounds[0].y, (double)this.zCoord + (double)bounds[0].z, (double)this.xCoord + (double)bounds[1].x, (double)this.yCoord + (double)bounds[1].y, (double)this.zCoord + (double)bounds[1].z);
   }

   public int getRenderingType() {
      return this.type;
   }
}
